<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <title>TOUPROPRE DBMS</title>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" media="screen" type="text/css" title="Design" href="styles/design.css" />
</head>

<body>
    <!-- TITLE  -->
	<div id="page_title">
      <h1>Client / Supplier Transfer System</h1>
    </div>

    <!-- Side Menues -->
    <?php include "tools/Menues.html"; ?>

    <!-- Main Body -->
    <div id="page_body">
<?php
/**
 * Includes
 */
include "tools/Queries.php";
include "tools/ColumnNames.php";

/**
 * Function Definitions
 */
function ShowTableSelectForm() {
    echo "\n<p class='text'>Please select the type of transfer and ID:</p>";
    echo "\n<form action='client_supplier_transfer.php' method='get'>";
    echo "\n<p>ID: <input type='text' name='id'/></p>";
    echo "\n<select name='table'>";
    echo "\n<option value='clients'>Client -> Supplier</option>";
    echo "\n<option value='suppliers'>Supplier -> Client</option>";
    echo "\n</select>";
    echo "\n<input type='submit' name='submit' value='Select'>";
    echo "\n</form>";
}

function ShowNewTransfer() {
    echo "\n<br/><a href=client_supplier_transfer.php>New Transfer</a>";
}

function BuildQuery($response) {
    $row = mysql_fetch_row($response);
    if($_GET['table']=='clients') {
        $sql = "INSERT INTO ".
          "suppliers".
          "(Name, VATnum, Address, PostCode, Town , Country, DateAdded, BankRupt)".
          "values".
          "('$row[1] $row[2]', $row[3], '$row[4]', $row[5], '$row[6]', '$row[7]', '$row[8]', 0)";
    }
    else if($_GET['table']=='suppliers') {
        $sql = "INSERT INTO ".
            "clients".
              "(FirstName, VATnum, Address, PostCode, Town , Country, DateAdded)".
            "values".
              "('$row[1]', $row[2], '$row[3]', $row[4], '$row[5]', '$row[6]', '$row[7]')";
    }
    else {
        $sql = "error";
    }
    return $sql;
}

/*
 * Behaviour:
 */
if(isset($_GET['table'])) {

    // Confirm Input :
    if(!isset($_GET['id']) OR $_GET['id'] == '') {
        echo "<p class='text'>Please enter a valid ID.</p>";
    }
    else if($_GET['submit']=='Select') {
        if($_GET['table']=='clients')         {$x = 'client';   $y = 'suppliers';}
        else if ($_GET['table']=='suppliers') {$x = 'supplier'; $y = 'clients';}
        echo "<p class='text'>Please confirm that this is the $x you want to copy ".
            "to the $y database. </p>";

        $key   = GetKeyFieldName($_GET['table']);
        $table = $_GET['table'];
        $id    = $_GET['id'];
        EchoSelectQuery("SELECT * FROM $table WHERE $key=$id");

        echo "\n<form action='client_supplier_transfer.php' method='get'>";
        echo "\n<input type='hidden' name='table' value=$table />";
        echo "\n<input type='hidden' name='id' value=$id />";
        echo "\n<input type='submit' name='submit' value='Confirm'/>";
        echo "\n</form>";
    }

    // Perform the transfer :
    else if($_GET['submit']=='Confirm') {
        include "tools/connect.php";
        
        $key   = GetKeyFieldName($_GET['table']);
        $table = $_GET['table'];
        $id    = $_GET['id'];
        $sql = "SELECT * FROM $table WHERE $key=$id";
        echo "<p class=sql_cmd>MySQL > $sql</p>";
        $response = mysql_query($sql);
        
        $sql = BuildQuery($response);
        $response = mysql_query($sql);
        if(!$response) {
            echo "<p class=sql_error>MySQL > ".mysql_error()."</p>";
        }
        else {
            echo "<p class=sql_cmd>MySQL > $sql</p>";
            echo "<p class=text>Transfer complete.</p>";
        }
        echo "<br/>";
        ShowNewTransfer();
        
        mysql_close();
    }
}

// Ask for input :
else {
    ShowTableSelectForm();
}
?>
        
    </div>
    
    <!-- Footer -->
    <?php include "tools/Footer.html"; ?>
</body>
</html>
